package com.ysd.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.ysd.dao.impl.ZXXiangQingDaoImpl;
import com.ysd.entity.XiangQing;
import com.ysd.util.BaseServlet;
import com.ysd.util.Result;


public class AdminZiXunXiangQingServlet extends BaseServlet {
	/**
	 * 资讯多条件查询
	 * localhost:8080/shangcheng/adminZiXunXiangQing?reqName=getZiXunXiangQing
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getZiXunXiangQing(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int pId = 0;
		if(request.getParameter("zXXiangQingID")!=null && !request.getParameter("zXXiangQingID").equals("")) {
			pId = Integer.parseInt(request.getParameter("zXXiangQingID"));
		}
		
		String pName = request.getParameter("zXBiaoTi");
		String imgs = request.getParameter("zXNeiRong");
		String zxnr=request.getParameter("zXBTNeiRong");
		
		int zXFenLeild=0;
		if(request.getParameter("zXFenLeiId")!=null && !request.getParameter("zXFenLeiId").equals("")) {
			zXFenLeild = Integer.parseInt(request.getParameter("zXFenLeiId"));
		}
		int page=0;
		int size=0;
		if(request.getParameter("page")!=null && !request.getParameter("page").equals("")) {
			page=Integer.parseInt(request.getParameter("page"));
		}
		if(request.getParameter("size")!=null && !request.getParameter("size").equals("")) {
			size=Integer.parseInt(request.getParameter("size"));
		}
		
		XiangQing z = new XiangQing(pId, pName, imgs, zxnr,zXFenLeild,"");
		
		ZXXiangQingDaoImpl zxxqDao = new ZXXiangQingDaoImpl();
		List<XiangQing> zList = zxxqDao.getXiangQingsList(z, page, size);
		
		Gson gson = new Gson();
		String res = gson.toJson(zList);

		response.setCharacterEncoding("UTF-8");
		PrintWriter out =response.getWriter();
		
		out.print(res);
		
		out.flush();
		out.close();
	}
	/**
	 * 资讯多条件查询
	 * localhost:8080/shangcheng/adminZiXunXiangQing?reqName=getZiXunXiangQingSize
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getZiXunXiangQingSize(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int pId = 0;
		if(request.getParameter("zXXiangQingID")!=null && !request.getParameter("zXXiangQingID").equals("")) {
			pId = Integer.parseInt(request.getParameter("zXXiangQingID"));
		}
		
		String pName = request.getParameter("zXBiaoTi");
		String imgs = request.getParameter("zXNeiRong");
		String zxnr=request.getParameter("zXBTNeiRong");
		
		int zXFenLeild=0;
		if(request.getParameter("zXFenLeiId")!=null && !request.getParameter("zXFenLeiId").equals("")) {
			zXFenLeild = Integer.parseInt(request.getParameter("zXFenLeiId"));
		}
		
		
		XiangQing z = new XiangQing(pId, pName, imgs, zxnr,zXFenLeild,"");
		
		ZXXiangQingDaoImpl zxxqDao = new ZXXiangQingDaoImpl();
		List<XiangQing> zList = zxxqDao.getXiangQingSize(z);
		
		Gson gson = new Gson();
		String res = gson.toJson(zList.size());

		response.setCharacterEncoding("UTF-8");
		PrintWriter out =response.getWriter();
		
		out.print(res);
		
		out.flush();
		out.close();
	}
	
	/**
	 * localhost:8080/shangcheng/adminZiXunXiangQing?reqName=addZXXiangQing
	 * 添加资讯
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void addZXXiangQing(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out =response.getWriter();
		
		String zXBiaoTi = request.getParameter("zXBiaoTi");
		String zXBTNeiRong=request.getParameter("zXBTNeiRong");
		String zXNeiRong = request.getParameter("zXNeiRong");
		
		int zXFenLeild=0;
		if(request.getParameter("zXFenLeiId")!=null && !request.getParameter("zXFenLeiId").equals("")) {
			zXFenLeild = Integer.parseInt(request.getParameter("zXFenLeiId"));
		}
		
		XiangQing zxxq= new XiangQing(0, zXBiaoTi, zXBTNeiRong, zXNeiRong, zXFenLeild);
		System.out.println(zxxq);
		ZXXiangQingDaoImpl zxDao = new ZXXiangQingDaoImpl();
		int n=zxDao.addziXunXiangQing(zxxq);
		if(n>0){
			out.print(Result.toClient(true, "添加成功",""));
		}else {
			out.print(Result.toClient(false, "添加失败",""));
		}
		
		out.flush();
		out.close();
	}
	
	/**
	 * localhost:8080/shangcheng/adminZiXunXiangQing?reqName=getZXXiangQingId
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void getZXXiangQingId(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out =response.getWriter();
		
		ZXXiangQingDaoImpl zxDao = new ZXXiangQingDaoImpl();
		int Id = Integer.parseInt(request.getParameter("Id"));
		XiangQing zx = zxDao.getZiXunXiangQingID(Id);
		
		Gson gson = new Gson();
		String res = gson.toJson(zx);
		out.print(res);
		
		out.flush();
		out.close();
	}
	
	/**
	 * localhost:8080/shangcheng/adminZiXunXiangQing?reqName=xiuGaiZXXiangQingId
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void xiuGaiZXXiangQingId(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out =response.getWriter();
		
		ZXXiangQingDaoImpl zxDao = new ZXXiangQingDaoImpl();
		
		int pId = Integer.parseInt(request.getParameter("zXXiangQingID"));
		String pName = request.getParameter("zXBiaoTi");
		String imgs = request.getParameter("zXNeiRong");
		String zxnr=request.getParameter("zXBTNeiRong");
		int zXFenLeild=0;
		if(request.getParameter("zXFenLeiId")!=null && !request.getParameter("zXFenLeiId").equals("")) {
			zXFenLeild = Integer.parseInt(request.getParameter("zXFenLeiId"));
		}
		XiangQing xgzx = new XiangQing(pId, pName, imgs, zxnr,zXFenLeild);
		
		int n = zxDao.xiuGaiZiXun(xgzx);
		if(n>0){
			out.print(Result.toClient(true, "修改成功",""));
		}else {
			out.print(Result.toClient(false, "修改失败",""));
		}
		
		out.flush();
		out.close();
	}
	
	/**
	 * 资讯详情
	 * localhost:8080/shangcheng/adminZiXunXiangQing?reqName=delZXXiangQing
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void delZXXiangQing(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		int cateId = Integer.parseInt(request.getParameter("cateId"));
		
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out =response.getWriter();
		
		ZXXiangQingDaoImpl zxxqDao = new ZXXiangQingDaoImpl();
		int n=zxxqDao.delzXXiangQingID(cateId);
		
		if(n>0){
			out.print(Result.toClient(true, "删除成功",""));
		}else {
			out.print(Result.toClient(false, "删除失败",""));
		}
		
		out.flush();
		out.close();
	}
	/**
	 * 批量删除
	 * localhost:8080/shangcheng/adminZiXunXiangQing?reqName=delzXXiangQingIDs
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
public void delzXXiangQingIDs(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String zXXiangQingIDs = request.getParameter("zXXiangQingIDs");
		
		response.setCharacterEncoding("UTF-8");
		PrintWriter out =response.getWriter();
		
		ZXXiangQingDaoImpl zxxqDao = new ZXXiangQingDaoImpl();
		int n=zxxqDao.delzXXiangQingIDs(zXXiangQingIDs);
		
		if(n>0){
			out.print(Result.toClient(true, "删除成功",""));
		}else {
			out.print(Result.toClient(false, "删除失败",""));
		}
		
		out.flush();
		out.close();
	}
	

}
